package org.javaboy.consumer01.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 头部交换机
 */
@Configuration
public class HeaderConfig {
    public static final String HEADERS_EXCHANGE = "headers.exchange";
    public static final String HEADERS_NAME_QUEUE = "headers_name_queue";
    public static final String HEADERS_AGE_QUEUE = "headers_age_queue";

    @Bean
    HeadersExchange headersExchange() {
        return new HeadersExchange(HEADERS_EXCHANGE, true, false);
    }

    @Bean
    Binding nameBinding() {
        return BindingBuilder.bind(nameQueue())
                .to(headersExchange())
                //表示消息头中要有 name 字段
                .where("name")
                //表示 name 字段的值等于 zhangsan
                .matches("zhangsan");
    }

    @Bean
    Binding ageBinding() {
        return BindingBuilder.bind(ageQueue())
                .to(headersExchange())
                //表示消息头中要有 age 字段
                .where("age")
                //只要有 age 字段，就匹配
                .exists();
    }

    @Bean
    Queue ageQueue() {
        return new Queue(HEADERS_AGE_QUEUE, true, false, false);
    }

    @Bean
    Queue nameQueue() {
        return new Queue(HEADERS_NAME_QUEUE, true, false, false);
    }

}
